﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.TcCommands.AWCTeamcenterNavigator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using ActiveWorkspaceHosting;
using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.TcCommands;

public class AWCTeamcenterNavigator : TxWindow, IComponentConnector
{
  protected CApAWCHostSessionManager m_hostManager;
  private double DebugSessionTimeOut = 600000.0;
  private bool m_isStudyManager;
  private bool m_shouldCloseDialog;
  internal AWCTeamcenterNavigator ctrl;
  internal Grid m_grid;
  internal TxActiveWorkspaceWebHostControl m_hostControl;
  private bool _contentLoaded;

  public AWCTeamcenterNavigator(CApAWCHostSessionManager hostManager)
  {
    this.m_hostManager = hostManager;
    this.m_isStudyManager = this.m_hostManager is CApAWCManager;
    this.InitializeComponent();
  }

  public bool ShouldCloseDialog
  {
    get => this.m_shouldCloseDialog;
    set
    {
      if (!this.m_isStudyManager)
        return;
      this.m_shouldCloseDialog = value;
    }
  }

  public void HideDialog()
  {
    this.SetSemiModal(false);
    ((UIElement) this).Visibility = Visibility.Collapsed;
    ((UIElement) this).IsEnabled = false;
  }

  public void ShowHiddenDialog()
  {
    ((UIElement) this).IsEnabled = true;
    ((UIElement) this).Visibility = Visibility.Visible;
    this.SetSemiModal(true);
  }

  protected virtual void RegisterManagerServicesAndEventsInternal()
  {
  }

  protected virtual void UnitializeInternal() => this.m_hostControl.Uninitialize();

  private void SetSemiModal(bool isSemimodal)
  {
    this.SemiModal = isSemimodal;
    TxApplication.CommandsManager.DisableAllCommands = isSemimodal;
  }

  private bool IsDialogHidden()
  {
    // ISSUE: explicit non-virtual call
    return !__nonvirtual (((UIElement) this).IsEnabled) && ((UIElement) this).Visibility == Visibility.Collapsed;
  }

  private void ctrl_Loaded(object sender, RoutedEventArgs e)
  {
    string targetUri = this.m_hostManager.GetTargetUri();
    if (this.m_hostControl.IsValidTarget(targetUri))
    {
      this.RegisterManagerServicesAndEvents();
      this.IncreaseTimeOutWhenInDebugSession();
      this.m_hostControl.NavigateToTarget(targetUri);
      this.ShouldCloseDialog = this.m_hostManager.IsDebugSession || ((UIElement) this.m_hostControl).Visibility != 0;
    }
    else
    {
      this.ShouldCloseDialog = true;
      this.m_hostControl.ShowError(DnProcessSimulateCommands.TcCommands.TcNavigator.StringTable.AWC_INVALID_SERVER_ADDRESS_ERROR);
    }
  }

  private void IncreaseTimeOutWhenInDebugSession()
  {
    if (!this.m_hostManager.IsDebugSession)
      return;
    this.m_hostControl.IncreaseTimeOut(this.DebugSessionTimeOut);
  }

  private void RegisterManagerServicesAndEvents()
  {
    this.m_hostManager.RegisterServices();
    TxActiveWorkspaceHostManagerEx.Instance.ClientCompleted += new EventHandler<TxClientCompletedEventArgs>(this.Instance_ClientCompleted);
    this.RegisterManagerServicesAndEventsInternal();
    TxApplication.ApplicationEvents.Exiting += new TxApplication_ExitingEventHandler(this.ApplicationEvents_Exiting);
  }

  private void Instance_ClientCompleted(object sender, TxClientCompletedEventArgs e)
  {
    bool flag = true;
    if (this.m_isStudyManager)
    {
      CApAWCManager hostManager = this.m_hostManager as CApAWCManager;
      if (!hostManager.IsDuringLoadStudy && !this.IsDialogHidden())
      {
        if (hostManager.LoadStudyWasCanceled)
        {
          ((UIElement) this.m_hostControl).Refresh();
          hostManager.LoadStudyWasCanceled = false;
          flag = false;
        }
      }
      else
        flag = false;
    }
    if (!flag)
      return;
    this.m_hostManager.OnClientCompleted();
    if (this.m_isStudyManager)
    {
      flag = !(this.m_hostManager as CApAWCManager).LoadStudyWasCanceled;
      (this.m_hostManager as CApAWCManager).LoadStudyWasCanceled = false;
    }
    if (!flag)
      return;
    ((Window) this).Close();
  }

  private void UnregisterManagerServicesAndEvents()
  {
    this.UnitializeInternal();
    TxActiveWorkspaceHostManagerEx.Instance.ClientCompleted -= new EventHandler<TxClientCompletedEventArgs>(this.Instance_ClientCompleted);
    this.m_hostManager.UnregisterServices();
    TxApplication.ApplicationEvents.Exiting -= new TxApplication_ExitingEventHandler(this.ApplicationEvents_Exiting);
  }

  private void ctrl_Closing(object sender, CancelEventArgs e)
  {
    if (this.m_isStudyManager && !this.ShouldCloseDialog)
    {
      this.HideDialog();
      e.Cancel = true;
    }
    else
    {
      if (this.m_hostManager == null)
        return;
      this.UnregisterManagerServicesAndEvents();
    }
  }

  private void ApplicationEvents_Exiting(object sender, TxApplication_ExitingEventArgs args)
  {
    if (!this.m_isStudyManager)
      return;
    this.m_shouldCloseDialog = true;
    ((Window) this).Close();
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/tccommands/tcnavigator/awcteamcenternavigator.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        this.ctrl = (AWCTeamcenterNavigator) target;
        ((Window) this.ctrl).Closing += new CancelEventHandler(this.ctrl_Closing);
        ((FrameworkElement) this.ctrl).Loaded += new RoutedEventHandler(this.ctrl_Loaded);
        break;
      case 2:
        this.m_grid = (Grid) target;
        break;
      case 3:
        this.m_hostControl = (TxActiveWorkspaceWebHostControl) target;
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
